<?php

/**
 * This is the model class for table "product".
 *
 * The followings are the available columns in table 'product':
 * @property string $product_id
 * @property string $category_id
 * @property string $title
 * @property string $brand
 * @property string $description
 *
 * The followings are the available model relations:
 * @property Item[] $items
 * @property Category $category
 */
class Product extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Product the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'product';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('category_id', 'length', 'max' => 10),
            array('title', 'length', 'max' => 50),
            array('brand', 'length', 'max' => 40),
            array('description', 'length', 'max' => 100),
            array('title, category_id', 'required'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('product_id, category_id, title, brand', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'items' => array(self::HAS_MANY, 'Item', 'product_id'),
            'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'product_id' => ProductModule::t('Id'),
            'category_id' => ProductModule::t('Category'),
            'title' => ProductModule::t('Title'),
            'brand' => ProductModule::t('Brand'),
            'description' => ProductModule::t('Description')
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('product_id', $this->product_id, true);
        $criteria->compare('category_id', $this->category_id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('brand', $this->brand, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function listProduct()
    {
        $criteria = new CDbCriteria;

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'product_id desc'
                    ),
                    'pagination' => array(
                        'pageSize' => 5
                    ),
                ));
    }

    public function findIDByTitle($title)
    {
        $criteria = new CDbCriteria;
        $criteria->compare('title', trim($title));
        $product = $this->find($criteria);
        return (!empty($product)) ? $product->product_id : 1;
    }

}